<?php
namespace app\admin\controller;
use think\Validate;
class Auth
{
	public function __construct(){
		Admin_auth();
	}
	
    public function index()
    {
		$pid = input('get.pid');
		$phone = input('get.phone');
		if(!empty($pid)){
			$where = ['id'=>['>',0],'type'=>1,'pid'=>$pid];
		}else{
			$where = ['id'=>['>',0]];
		}
		if(!empty($phone)){
			$where = ['id'=>['>',1],'phone'=>$phone];
		}
		$pages = pages('admini',$where,input('get.page'));
		$data = db("admini")->where($where)->limit($pages['limit'])->order('id desc')->select();
        return view('',['data'  => $data,'pages' => $pages['pages']]);
    }
	
	public function edit()
    {
		$id = input("get.id")?input("get.id"):0;
		$data = array();
		$menu = array();
		$province = db("province")->where('status',1)->select();
		$category = db("service_category")->where('gid',0)->select();
		$topmenu = db("admin_menu")->where('gid',0)->select();
		if(!empty($topmenu)){
			foreach($topmenu as $sub){
				$sub['sub'] = db("admin_menu")->where('gid',$sub['id'])->select();
				Array_push($menu,$sub); 
			}
		}
		if(request()->isPost()){
			$data = input("post.");
			if($data['type']==1){
				$validate = new Validate([
					['auth','require',lang('label')['auth'].lang('currency')['required']],
				]);
				$data['auth'] = implode(',',$data['auth']);
				$data['category'] = implode(',',$data['category']);
			}
			if($data['type']==2){
				$validate = new Validate([
					['pid','require',lang('currency')['city'].lang('currency')['required']],
					['auth','require',lang('label')['auth'].lang('currency')['required']],
				]);
				foreach($data['pid'] as $pid){
					$city = db('city')->where(['father_id'=>$pid])->column('id');
					$data['pid'] = array_merge($data['pid'],$city);
					if(!empty($city)){
						foreach($city as $cid){
							$area = db('area')->where(['father_id'=>$cid])->column('id');
							$data['pid'] = array_merge($data['pid'],$area);
						}
					}
				}
				$data['auth'] = implode(',',$data['auth']);
				$data['pid'] = implode(',',$data['pid']);
			}
			if (!$validate->check($data)) {
				return error($validate->getError());
			}
			if(db("admini")->where(['phone' => $data['phone']])->where('id','<>',$id)->find()){
				return error(lang('tip')['re_phone']);
				exit;
			}
			if($id){
				if(empty($data['password'])){
					unset($data['password']); 
				}else{
					$data['password'] = md5($data['password']);
				}
				db("admini")->where(['id' => $id])->update($data);
				$data['id'] = $id;
				$data['password'] = '';
			}else{
				$data['time'] = time();
				$data['password'] = md5($data['password']);
				$data['id'] = db("admini")->insertGetId($data);
			}
		}
		$data = db("admini")->where(['id'=>['>',1],'id' => $id])->find();
        return view('',['data'  => $data,'menu' => $menu,'province' => $province,'category' => $category]);
    }
	
	public function del()
    {
		$id = input("get.id")?input("get.id"):0;
		$page = input("get.page");
		$data = db("admini")->where(['type' => '1'])->delete($id);
		redirect(url('/admin/auth?page='.$page));
		exit;
    }
}
